package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult getAllSetMeals(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = (Page<SetmealVO>) setmealMapper.getAllSetMeals(setmealPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return  pageResult;
    }

    @Override
    public void setSetmealStatus(Long id, Integer status) {
        setmealMapper.setSetmealStatus(id,status);
    }

    @Transactional
    @Override
    public void addNewSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addNewSetmeal(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish s : list) {
            s.setSetmealId(id);
        }
        log.info(list.toString());
        setmealDishMapper.addNewSetDish(list);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.getSetmealById(id);
        List<SetmealDish> list = setmealDishMapper.getSetmealDishById(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if (list!=null&&list.size()>0){
            for (SetmealDish sd : list) {
                sd.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.deleteSetmeal(setmeal.getId());
            setmealDishMapper.addNewSetDish(list);
        }else {
            throw new SetmealEnableFailedException("套餐菜品不存在");
        }

    }

    @Transactional
    @Override
    public void deleteSetmeal(List<Long> ids) {
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.getSetmealById(id);
            if (setmealVO.getStatus() == StatusConstant.DISABLE){
                setmealMapper.deleteSetmeal(id);
            }
            else {
                throw new SetmealEnableFailedException("售卖中套餐无法删除");
            }
            setmealDishMapper.deleteSetmeal(id);
        }

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
